const { stringify } = require("querystring");
const { Database } = require("sqlite3");

function fexpert(){
    const sqlite3 = require("sqlite3").verbose()
    const path = require('path');
    const dbName = path.join(__dirname, '../data/demo.db');
    var db;
    db = new sqlite3.Database(dbName,function(err){
        if(err) throw err;
    })
    db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=1))),e_id FROM qone WHERE c_id=1))
     WHERE c_id=1
    `,function(err,row){
        if(err) throw err; 
    })
    db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=2))),e_id FROM qone WHERE c_id=2))
    WHERE c_id=2
   `,function(err,row){
       if(err) throw err; 
   })
   db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=3))),e_id FROM qone WHERE c_id=3))
   WHERE c_id=3
  `,function(err,row){
      if(err) throw err; 
  })
  db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=4))),e_id FROM qone WHERE c_id=4))
  WHERE c_id=4
 `,function(err,row){
     if(err) throw err; 
 })
 db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=5))),e_id FROM qone WHERE c_id=5))
 WHERE c_id=5
`,function(err,row){
    if(err) throw err; 
})
db.run(` UPDATE cafe SET e_id=(SELECT e_id   FROM (SELECT MAX(ABS(score-(SELECT q1ave FROM cafe WHERE c_id=6))),e_id FROM qone WHERE c_id=6))
WHERE c_id=6
`,function(err,row){
   if(err) throw err; 
})
}

module.exports=fexpert;